package com.zdp.controller;

import com.zdp.common.domain.ResultInfo;
import com.zdp.common.enums.YesOrNo;
import com.zdp.common.util.JsonUtils;
import com.zdp.common.util.ResultInfoUtil;
import com.zdp.config.RedisOperator;
import com.zdp.pojo.Carousel;
import com.zdp.pojo.Category;
import com.zdp.pojo.vo.CategoryVO;
import com.zdp.pojo.vo.NewItemsVO;
import com.zdp.service.CarouselService;
import com.zdp.service.CategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sesshomaru
 * @date 2021/7/30
 * 首页
 */
@RestController
@RequestMapping("index")
public class IndexController {

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisOperator redisOperator;

    /**
     * 轮播图
     * @return
     */
    @GetMapping("/carousel")
    public ResultInfo carousel() {
        String carouselStr = redisOperator.get("carousel");
        List<Carousel> carousels = new ArrayList<>();
        if (StringUtils.isBlank(carouselStr)) {
            carousels = carouselService.queryAll(YesOrNo.YES.type);
            redisOperator.set("carousel", JsonUtils.objectToJson(carousels));
        } else {
            carousels = JsonUtils.jsonToList(carouselStr, Carousel.class);
        }
        return ResultInfoUtil.buildSuccess(carousels);
    }

    /**
     * 查询商品的一级分类
     * @return
     */
    @GetMapping("/cats")
    public ResultInfo cats() {
        String cat = redisOperator.get("cat");
        List<Category> categories = new ArrayList<>();
        if (StringUtils.isBlank(cat)) {
            categories = categoryService.queryAllRootLevelCat();
            redisOperator.set("cat", JsonUtils.objectToJson(categories));
        } else {
            categories = JsonUtils.jsonToList(cat, Category.class);
        }
        return ResultInfoUtil.buildSuccess(categories);
    }

    /**
     * 获取商品子分类
     * @param rootCatId 一级分类id
     * @return
     */
    @GetMapping("/subCat/{rootCatId}")
    public ResultInfo subCat(@PathVariable Integer rootCatId) {
        if (rootCatId == null) {
            return ResultInfoUtil.buildError("分类不存在");
        }
        List<CategoryVO> list = new ArrayList<>();
        String key = "subCat" + rootCatId;
        String CategoryStr = redisOperator.get(key);
        if (StringUtils.isBlank(CategoryStr)) {
            list = categoryService.getSubCatList(rootCatId);
            redisOperator.set(key, JsonUtils.objectToJson(list));
        } else {
            list = JsonUtils.jsonToList(CategoryStr, CategoryVO.class);
        }
        return ResultInfoUtil.buildSuccess(list);
    }

    /**
     * 查询每个一级分类下的最新6条商品数据
     * @param rootCatId 一级分类id
     * @return
     */
    @GetMapping("/sixNewItems/{rootCatId}")
    public ResultInfo sixNewItems(
            @PathVariable Integer rootCatId) {

        if (rootCatId == null) {
            return ResultInfoUtil.buildError("分类不存在");
        }

        List<NewItemsVO> list = categoryService.getSixNewItemsLazy(rootCatId);
        //List<NewItemsVO> list = new ArrayList<>();
        return ResultInfoUtil.buildSuccess(list);
    }
}
